<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style type="text/css">
        .item
        {
            border: 1px solid black;
            height: 200px;
            width: 200px;
            margin-top: 20px;
            margin-left: 20px;
            float: left;
        }
    </style>
    <script type="text/javascript">
        window.onload=function()
        {
            //获取div元素对象
            let items=document.getElementsByClassName("item");
            //遍历并且绑定事件
            for(let i=0;i<items.length;i++)
            {
                items[i].onclick=function()
                {
                    items[i].style.backgroundColor="red";
                };
            }
        };
    </script>
</head>
<body>
    <div class="container">
        <h2 class="page-header">点击切换颜色</h2>
        <div class="item"></div>
        <div class="item"></div>
        <div class="item"></div>
    </div>
</body>
</html>